<?php

/**
 * 新闻控制器
 */
class NewsController extends Controller
{
	/**
	 * 新闻资讯
	 */
	public function actionIndex()
	{
		$this->redirect('/news/list');
	}

	/**
	 *新闻资讯列表
     */
	public function actionList(){
		$per=15;

		$id=intval($_GET['id']);
		$type_id=intval($_GET['type_id']);
		$lang=$this->getLang();
		if($lang=='en'){
			$this->pageTitle='News'.$this->public_title['en'];
		}else{
			$this->pageTitle='新闻资讯'.$this->public_title['cn'];
		}
		$type_data=news_type::model()->getTypeData($lang);


		if($id>0){$where=" and `id`='$id'";}
		if($type_id>0){$where=" and `type_id`='$type_id'";}


		$sql="SELECT count(`id`) as `cnt` FROM `news_info` WHERE 1=1 $where ";
		$model=Yii::app()->db->createCommand($sql)->queryAll();
		$cnt=$model[0]['cnt'];

		$page = new PaginationWeb($cnt,$per);
		$page_list = $page->fpage(array(0,1,2,3,4,5,6,7,8));

		if($lang=='en'){
			$sql="SELECT *,`en_title` as `title_name`,`en_body` as `body_name` FROM `news_info` WHERE 1=1 $where ORDER BY `type_id` ASC,`id` desc  $page->limit";
		}else{
			$sql="SELECT *,`cn_title` as `title_name`,`cn_body` as `body_name` FROM `news_info` WHERE 1=1 $where ORDER BY `type_id` ASC,`id` desc   $page->limit";
		}
		$info_data=Yii::app()->db->createCommand($sql)->queryAll();

		$this->render('news/list',array('type_data'=>$type_data,'info_data'=>$info_data,'page_list'=>$page_list));
	}

	/**
	 *产品详情
	 */
	public function actionInfo(){
		$per=15;

		$id=intval($_GET['id']);
		$type_id=intval($_GET['type_id']);
		$lang=$this->getLang();
		if($lang=='en'){
			$this->pageTitle='News'.$this->public_title['en'];
		}else{
			$this->pageTitle='新闻资讯'.$this->public_title['cn'];
		}
		$type_data=news_type::model()->getTypeData($lang);


		if($id>0){$where=" and `id`='$id'";}
		if($type_id>0){$where=" and `type_id`='$type_id'";}


		if($lang=='en'){
			$sql="SELECT *,`en_title` as `title_name`,`en_body` as `body_name` FROM `news_info` WHERE 1=1 $where limit 1";
		}else{
			$sql="SELECT *,`cn_title` as `title_name`,`cn_body` as `body_name` FROM `news_info` WHERE 1=1 $where limit 1";
		}
		$info_data=Yii::app()->db->createCommand($sql)->queryAll();

		$this->render('news/info',array('type_data'=>$type_data,'info_data'=>$info_data[0]));
	}








}